#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 1005;

int a, b, n;
int x[N][N], t[N][N], mn[N][N], mx[N][N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> a >> b >> n;
  rep(i, 1, a) rep(j, 1, b) cin >> x[i][j];
  rep(i, 1, a) {  // 处理行
    deque<int> dq;
    rep(j, 1, b) {
      while (dq.size() && x[i][dq.back()] >= x[i][j]) dq.pop_back();
      while (dq.size() && dq.front() + n <= j) dq.pop_front();
      dq.push_back(j);
      t[i][j] = x[i][dq.front()];
    }
  }
  rep(j, 1, b) {
    deque<int> dq;
    rep(i, 1, a) {
      while (dq.size() && t[dq.back()][j] >= t[i][j]) dq.pop_back();
      while (dq.size() && dq.front() + n <= i) dq.pop_front();
      dq.push_back(i);
      mn[i][j] = t[dq.front()][j];
    }
  }
  rep(i, 1, a) {
    deque<int> dq;
    rep(j, 1, b) {
      while (dq.size() && x[i][dq.back()] <= x[i][j]) dq.pop_back();
      while (dq.size() && dq.front() + n <= j) dq.pop_front();
      dq.push_back(j);
      t[i][j] = x[i][dq.front()];
    }
  }
  rep(j, 1, b) {
    deque<int> dq;
    rep(i, 1, a) {
      while (dq.size() && t[dq.back()][j] <= t[i][j]) dq.pop_back();
      while (dq.size() && dq.front() + n <= i) dq.pop_front();
      dq.push_back(i);
      mx[i][j] = t[dq.front()][j];
    }
  }
  int ans = 1e9;
  rep(i, n, a) rep(j, n, b) ans = min(ans, mx[i][j] - mn[i][j]);
  cout << ans;
  return 0;
}